/**
* 基础菜单 商品管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文件管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 上传文件 -->
    <div class="content">
      <el-form :inline="true" :model="formInline" class="user-search">
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">上传文件</el-button>
        </el-form-item>
        <!-- classType 下拉框 -->
        <el-form-item label="班型" prop="classType">
          <el-select v-model="formInline.classType" placeholder="请选择班型">
            <el-option key="" label="请选择" value=""></el-option>
            <!-- 根据实际的数据结构和数据源修改 options -->
            <el-option v-for="option in classTypeList" :key="option.itemCode" :label="option.itemName" :value="option.itemCode"></el-option>
          </el-select>
        </el-form-item>
        <!-- fileType 下拉框 -->
        <el-form-item label="类型" prop="fileType">
          <el-select v-model="formInline.fileType" placeholder="请选择类型">
            <el-option key="" label="请选择" value=""></el-option>
            <!-- 根据实际的数据结构和数据源修改 options -->
            <el-option v-for="option in fileTypeList" :key="option.itemCode" :label="option.itemName" :value="option.itemCode"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示位置" prop="displayPosition">
          <el-select v-model="formInline.displayPosition" placeholder="请选择显示位置">
            <el-option key="" label="请选择" value=""></el-option>
            <!-- 根据实际的数据结构和数据源修改 options -->
            <el-option v-for="option in displayPositionList" :key="option.itemCode" :label="option.itemName"
                       :value="option.itemCode"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input size="small" v-model="formInline.search" placeholder="文件名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
              element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="fileName" label="文件名称" width="180">
      </el-table-column>
      <el-table-column sortable prop="fileSizeMb" label="文件大小" width="160">
      </el-table-column>
      <el-table-column sortable prop="classTypeName" label="班型" width="160">
      </el-table-column>
      <el-table-column sortable prop="fileTypeName" label="类型" width="160">
      </el-table-column>
      <el-table-column sortable prop="displayPositionName" label="显示位置" width="160">
      </el-table-column>
      <el-table-column sortable prop="createdByName" label="上传者" width="200">
      </el-table-column>
      <el-table-column sortable prop="dateCreated" label="上传时间" width="220">
        <template slot-scope="scope">
          <div>{{ scope.row.dateCreated }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="checkFile(scope.row)">查看</el-button>
          <el-button size="mini" @click="handleDownload(scope.row)">下载</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.row.id, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">

        <!-- classType 下拉框 -->
        <el-form-item label="班型" prop="classType">
          <el-select v-model="editForm.classType" placeholder="请选择班型">
            <el-option key="" label="请选择" value=""></el-option>
            <!-- 根据实际的数据结构和数据源修改 options -->
            <el-option v-for="option in classTypeList" :key="option.itemCode" :label="option.itemName" :value="option.itemCode"></el-option>
          </el-select>
        </el-form-item>
        <!-- fileType 下拉框 -->
        <el-form-item label="类型" prop="fileType">
          <el-select v-model="editForm.fileType" placeholder="请选择类型">
            <el-option key="" label="请选择" value=""></el-option>
            <!-- 根据实际的数据结构和数据源修改 options -->
            <el-option v-for="option in fileTypeList" :key="option.itemCode" :label="option.itemName" :value="option.itemCode"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示位置" prop="displayPosition">
          <el-select v-model="editForm.displayPosition" placeholder="请选择显示位置">
            <el-option key="" label="请选择" value=""></el-option>
            <!-- 根据实际的数据结构和数据源修改 options -->
            <el-option v-for="option in displayPositionList" :key="option.itemCode" :label="option.itemName" :value="option.itemCode"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-upload style="" ref="upload" action="#" :on-preview="handlePreview" :on-remove="handleRemove"
                 :file-list="fileList"
                 :auto-upload="false" :on-change="handleSuccess">
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传</el-button>
      </el-upload>
      <!--            <div slot="footer" class="dialog-footer">-->
      <!--                <el-button size="small" @click="closeDialog">取消</el-button>-->
      <!--                <el-button size="small" type="primary" :loading="loading" class="title"-->
      <!--                    @click="submitForm('editForm')">保存</el-button>-->
      <!--                <el-button size="small" type="primary" :loading="loading" class="title"-->
      <!--                    @click="submitUpload">上传</el-button>-->
      <!--            </div>-->
    </el-dialog>
  </div>
</template>

<script>
import {deptList} from '../../api/userMG'
import {deptSaves, reqsDelete, downloadFile} from '../../aaa/index'
import Pagination from '../../components/Pagination'

export default {
  data() {
    return {
      fileList: [],
      rawFiles: [], // 新增一个数组来存储 raw 文件对象
      formData: new FormData(),
      selectedFiles: [],  //上传文件
      // formData: new FormData(),
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        classType: '',
        fileType: '',
        displayPosition: '',
        // token: localStorage.getItem('logintoken')
        token: sessionStorage.getItem('logintoken')
      },
      // rules表单验证
      rules: {
        deptName: [
          {required: true, message: '请输入部门名称', trigger: 'blur'}
        ],
        deptNo: [{required: true, message: '请输入部门代码', trigger: 'blur'}]
      },
      formInline: {
        pageNo: 1,
        pageSize: 10,
        classType: '',
        fileType:'',
        displayPosition:'',
        search: '',
        // token: localStorage.getItem('logintoken')
        token: sessionStorage.getItem('logintoken')
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: sessionStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },
      dictAll: [],//全部字典
      classTypeList: [],//班型
      fileTypeList: [],//文件类型
      displayPositionList: [],//显示位置
      uploading: false
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
    this.initDictAll();
  },
  methods: {
    initDictAll() {
      this.dictAll = JSON.parse(sessionStorage.getItem('dictAll'))
      //获取字段数据
      this.dictAll.forEach(it => {
        if (it.mainCode == 'class_type') {
          this.classTypeList = it.itemList;
        } else if (it.mainCode == 'file_type') {
          this.fileTypeList = it.itemList;
        } else if (it.mainCode == 'display_position') {
          this.displayPositionList = it.itemList;
        }
      })
    },
    submitUpload() {
      if (this.uploading==true){
        alert("正在上传，请不要继续操作！")
        return;
      }
      // 确保 fileList 不为空
      if (this.rawFiles.length === 0) {
        this.$message.error('请先选择文件');
        return;
      }
      if (this.editForm.classType === '') {
        this.$message.error('请先选择班型');
        return;
      }
      if (this.editForm.fileType === '') {
        this.$message.error('请先选择文件类型');
        return;
      }
      if (this.editForm.displayPosition === '') {
        this.$message.error('请先选择显示位置');
        return;
      }
      // 上传中
      this.uploading = true
      // 上传逻辑
      this.formData.append('classType', this.editForm.classType)
      this.formData.append('fileType', this.editForm.fileType)
      this.formData.append('displayPosition', this.editForm.displayPosition)
      this.rawFiles.forEach(file => {
        this.formData.append('files', file);
      });
      deptSaves(this.formData).then(res => {
        this.getdata(this.formInline)
        this.fileList = []
        this.rawFiles = []
        this.formData = new FormData()
        this.editFormVisible = false
        if (res.result == true) {
          this.$message({
            type: 'success',
            message: '数据保存成功！'
          })
          this.clearEditFormData()
          this.uploading = false
        } else {
          this.$message({
            type: 'info',
            message: res.msg
          })
          this.uploading = false
        }
      })
    },
    //清空editForm数据的方法
    clearEditFormData() {
      this.editForm = {
        classType: '',
        fileType: '',
        displayPosition: ''
      }
    },
    handleRemove(file, fileList) {
      this.rawFiles = fileList.map(file => file.raw);
      console.log(this.rawFiles);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleSuccess(response, file, fileList) {
      this.rawFiles = file.map(file => file.raw);
    },
    // 获取文件列表
    getdata(parameter) {
      this.loading = true
      deptList(parameter)
        .then(res => {
          console.log(res, '文件数据');
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.listData = res.data.list
            // 分页赋值
            this.pageparm.currentPage = this.formInline.pageNo
            this.pageparm.pageSize = this.formInline.pageSize
            this.pageparm.total = res.data.totalCount
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('加载失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.pageNo = parm.currentPage
      this.formInline.pageSize = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //下载
    handleDownload: function (row) {
      downloadFile(row.id)
    },
    // 查看
    checkFile(row) {
      const owList = ["doc","docx","rtf","xls","xlsx","ppt","pptx","pdf","wps","WPS","ofd","OFD","txt","js","json","css","sql","xml","java","cs"];
      if (owList.includes(row.fileSuffix)){
        var path = "https://ow365.cn/?i=33345&furl=" + row.showFilePath
        window.open(path, '_blank');
      }else{
        window.open(row.showFilePath, '_blank');
      }
    },
    //上传表单
    handleEdit(row) {
      this.editFormVisible = true
      this.title = '上传文件'
      this.editForm.classType = ''
      this.editForm.fileType = ''
      this.editForm.displayPosition = ''
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          deptSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '公司保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('公司保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除文件
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          reqsDelete(index)
            .then(res => {
              if (res.result) {
                this.$message({
                  type: 'success',
                  message: '文件已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('公司删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}

.content {
  margin: 20px;
  display: flex;
  align-items: center;
}

.content > .user-search {
  margin-left: 20px;
}
</style>


